package org.meb.conquest.web.ctrl;

import java.io.IOException;
import java.io.Serializable;

import javax.enterprise.context.SessionScoped;
import javax.faces.context.FacesContext;
import javax.inject.Named;

import lombok.Getter;
import lombok.Setter;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import twitter4j.Twitter;
import twitter4j.TwitterException;
import twitter4j.TwitterFactory;
import twitter4j.auth.RequestToken;

@Named
@SessionScoped
@Getter
@Setter
public class SignInCtrl implements Serializable {

	private static final long serialVersionUID = -3074865924489627523L;
	private static final Logger log = LoggerFactory.getLogger(SignInCtrl.class);

	public void signInWithTwitter() throws IOException, TwitterException {
		String consumerKey = System.getProperty("twitter4j.oauth.consumerKey");
		log.info("consumer key: {}", consumerKey);
		String consumerSecret = System.getProperty("twitter4j.oauth.consumerSecret");
		log.info("consumer secret: {}", consumerSecret);

		Twitter twitter = TwitterFactory.getSingleton();
		// twitter.setOAuthConsumer(consumerKey, consumerSecret);
		RequestToken requestToken = twitter.getOAuthRequestToken("www.google.com");
		FacesContext.getCurrentInstance().getExternalContext().redirect(requestToken.getAuthenticationURL());

		// FacesContext.getCurrentInstance().getExternalContext()
		// .redirect("https://api.twitter.com/oauth/authenticate?facesRedirect");
		// return "https://api.twitter.com/oauth/authenticate?facesRedirect";
	}
}
